package com.ck.exception;

import com.ck.exception.bean.Result;
import com.ck.exception.bean.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @Classname CkExceptionControllerAdvice
 * @Description 异常统一处理
 * @Date 2021/7/13 19:39
 * @Created by lpc
 */
@RestControllerAdvice
@Slf4j
public class CkExceptionControllerAdvice {



    public CkExceptionControllerAdvice() {
    }

    @ExceptionHandler({Exception.class})
    public Result exceptionHandler(Exception exception) {
        log.error("Exception异常：", exception);
        return Result.fail(exception.getMessage());
    }

    @ExceptionHandler({NullPointerException.class})
    public Result nullPointerExceptionHandler(NullPointerException nullPointerException) {
        log.error("NullPointerException异常：", nullPointerException);
        return Result.fail(ResultCode.PARAM_ERR.getCode(), ResultCode.PARAM_ERR.getMsg());
    }

    @ExceptionHandler({HttpMessageNotReadableException.class})
    public Result jsonParseExceptionHandler(HttpMessageNotReadableException exception) {
        log.error("HttpMessageNotReadableException异常：", exception);
        return Result.fail(ResultCode.PARAM_ERR.getCode(), ResultCode.PARAM_ERR.getMsg());
    }

    @ExceptionHandler({CkException.class})
    public Result serviceExceptionHandler(CkException exception) {
        log.error("CkException异常：", exception);
        return Result.fail(exception.getMessage());
    }


    @ExceptionHandler({MethodArgumentNotValidException.class})
    public Result methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
        log.error("对象参数校验错误：", exception);
        BindingResult result = exception.getBindingResult();
        List<FieldError> fieldErrors = result.getFieldErrors();
        StringBuilder builder = new StringBuilder();
        Iterator var5 = fieldErrors.iterator();

        while(var5.hasNext()) {
            FieldError error = (FieldError)var5.next();
            builder.append(error.getDefaultMessage()).append("\n");
        }

        return new Result(ResultCode.PARAM_ERR.getCode(), builder.toString());
    }

    @ExceptionHandler({ConstraintViolationException.class})
    public Result constraintViolationExceptionHandler(ConstraintViolationException exception) {
        log.error("单个参数校验错误：", exception);
        Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations();
        StringBuilder builder = new StringBuilder();
        Iterator iterator = constraintViolations.iterator();

        while(iterator.hasNext()) {
            builder.append(((ConstraintViolation)iterator.next()).getMessage()).append("\n");
        }

        return new Result(ResultCode.PARAM_ERR.getCode(), builder.toString());
    }

    @ExceptionHandler({MissingServletRequestParameterException.class})
    public Result missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException exception) {
        log.error("缺少参数校验错误：", exception);
        return new Result(ResultCode.PARAM_ERR.getCode(), ResultCode.PARAM_ERR.getMsg()  + exception.getParameterName());
    }

}
